package com.liaoban.app.ui.message;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.liaoban.app.R;
import com.liaoban.app.api.type.comment.BaseResponse;
import com.liaoban.app.api.type.event.SendGiftEvent;
import com.liaoban.app.api.type.request.ActiveCallEndReq;
import com.liaoban.app.api.type.request.CallFaQiReq;
import com.liaoban.app.api.type.request.SendGiftReq;
import com.liaoban.app.api.type.request.SendYuMoneyReq;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.constants.ConfigKey;
import com.liaoban.app.helper.ConfigHelper;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.mine.RechargeActivity;
import com.liaoban.app.util.RxUtils;
import com.liaoban.app.widget.MyAlertDialog;
import com.liaoban.app.widget.SendGiftDialog;
import com.netease.nim.avchatkit.activity.AVChatActivity;
import com.netease.nim.avchatkit.constant.CallStateEnum;
import com.netease.nim.avchatkit.controll.AVChatSoundPlayer;
import com.netease.nim.avchatkit.event.ChongZhiEvent;
import com.netease.nim.avchatkit.notification.AVChatNotification;
import com.netease.nim.avchatkit.ui.SendGift;
import com.netease.nimlib.sdk.avchat.constant.AVChatType;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.functions.Consumer;

/**
 * Created by ybc on 2018/8/13 0013.
 */
public class MyAVChatActivity extends AVChatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AVChatNotification.sNoticeClickClass = MyAVChatActivity.class;
        mCallBack = new CallBack() {
            @Override
            public void callFaQi(String imId, String callType) {
                CallFaQiReq callFaQiReq = new CallFaQiReq();
                callFaQiReq.setImId(imId);
                callFaQiReq.setCallType(callType);
                CommonBiz.getInstance().callFaQi(callFaQiReq).compose(RxUtils.ioToMainTread()).subscribe(callReqBaseResponse -> {
                    if (avChatAudioUI == null) {
                        return;
                    }
                    if (1 == callReqBaseResponse.code) {
                        mShenYuTime = callReqBaseResponse.data.getTime();
                        if ("1".equals(callType)) { //语音
                            AVChatSoundPlayer.instance().play(AVChatSoundPlayer.RingerTypeEnum.CONNECTING);
                            avChatAudioUI.doOutGoingCall(receiverId);
                        } else if ("2".equals(callType)) { //视频
                            AVChatSoundPlayer.instance().play(AVChatSoundPlayer.RingerTypeEnum.CONNECTING);
                            avChatVideoUI.doOutgoingCall(receiverId);
                        }
                    } else {
                        ToastHelper.show(MyAVChatActivity.this, callReqBaseResponse.msg);
                        finish();
                    }
                }, throwable -> {
                    if (avChatAudioUI == null) {
                        return;
                    }
                    ToastHelper.show(MyAVChatActivity.this, throwable.getMessage());
                });

            }

            @Override
            public void jieTong(String imId, String callType, String callId) {
                CallFaQiReq callFaQiReq = new CallFaQiReq();
                callFaQiReq.setImId(imId);
                callFaQiReq.setCallType(callType);
                callFaQiReq.setCallId(callId);
                CommonBiz.getInstance().callJieTong(callFaQiReq).compose(RxUtils.ioToMainTread()).subscribe();
            }

            @Override
            public void activeCallend(String imId, String callId, String endType, String callTime, String call_type) {
                ActiveCallEndReq activeCallEndReq = new ActiveCallEndReq();
                activeCallEndReq.setCall_id(callId);
                activeCallEndReq.setCall_time(callTime);
                activeCallEndReq.setIm_id(imId);
                activeCallEndReq.setEnd_type(endType);
                CommonBiz.getInstance().activeCallEnd(activeCallEndReq).compose(RxUtils.ioToMainTread()).subscribe();
            }

            @Override
            public void sendGift(int id, String callId, String callTime) {
                int resId = getResid(id);
                /*SendGiftDialogFragment.show(getSupportFragmentManager(), resId, getGiftName(id), v -> {
                    SendGiftReq sendGiftReq = new SendGiftReq();
                    sendGiftReq.setCall_id(callId);
                    sendGiftReq.setId(String.valueOf(id));
                    sendGiftReq.setCall_time(callTime);
                    CommonBiz.getInstance().sendGift(sendGiftReq).compose(RxUtils.ioToMainTread()).subscribe(sendGiftBaseResponse -> {
                        if (avChatAudioUI == null) {
                            return;
                        }
                        ToastHelper.show(MyAVChatActivity.this, sendGiftBaseResponse.msg);
                        if (1 == sendGiftBaseResponse.code) {
                            if (state == CallStateEnum.AUDIO.getValue()) { //语音
                                avChatAudioUI.sendGiftSuc(sendGiftBaseResponse.data);
                            } else {
                                avChatVideoUI.sendGiftSuc(sendGiftBaseResponse.data);
                            }
                        }
                    }, throwable -> {
                        if (avChatAudioUI == null) {
                            return;
                        }
                        ToastHelper.show(MyAVChatActivity.this, throwable.getMessage());
                    });
                }, null);*/
                new SendGiftDialog(MyAVChatActivity.this).builder().setMsg(getGiftName(id))
                        .setIconResId(resId).setPositiveButton("马上赠送", view -> {
                    SendGiftReq sendGiftReq = new SendGiftReq();
                    sendGiftReq.setCall_id(callId);
                    sendGiftReq.setId(String.valueOf(id));
                    sendGiftReq.setCall_time(callTime);
                    CommonBiz.getInstance().sendGift(sendGiftReq).compose(RxUtils.ioToMainTread()).subscribe(sendGiftBaseResponse -> {
                        if (avChatAudioUI == null) {
                            return;
                        }
                        ToastHelper.show(MyAVChatActivity.this, sendGiftBaseResponse.msg);
                        if (1 == sendGiftBaseResponse.code) {
                            if (state == CallStateEnum.AUDIO.getValue()) { //语音
                                avChatAudioUI.sendGiftSuc(sendGiftBaseResponse.data);
                            } else {
                                avChatVideoUI.sendGiftSuc(sendGiftBaseResponse.data);
                            }
                            mShenYuTime = sendGiftBaseResponse.data.getTime();
                        }
                    }, throwable -> {
                        if (avChatAudioUI == null) {
                            return;
                        }
                        ToastHelper.show(MyAVChatActivity.this, throwable.getMessage());
                    });
                }).setNegativeButton("", null).show();
            }

            @Deprecated
            @Override
            public void sendYuMoney(String callId, String callTime) {
                SendYuMoneyReq sendYuMoneyReq = new SendYuMoneyReq();
                sendYuMoneyReq.setCall_id(callId);
                sendYuMoneyReq.setCall_time(callTime);
                CommonBiz.getInstance().sendYuMoney(sendYuMoneyReq).compose(RxUtils.ioToMainTread()).subscribe(baseResponse -> {
                    if (avChatAudioUI == null) {
                        return;
                    }
                    int time = baseResponse.data.getTime();
                    /*if (time < 120) {
                        ToastHelper.show(MyAVChatActivity.this, "余额不足，请尽快充值");
                    }*/
                    if (time <= 0) { //挂断
                        avChatAudioUI.doHangUp();
                        avChatVideoUI.doHangUp();
                    }
                });
            }
        };
        String wuserStatus = ConfigHelper.getString(this, ConfigKey.USER_WUSER_STATUS);
        String tel = ConfigHelper.getString(this, ConfigKey.USER_TEL);
        if ("1".equals(wuserStatus) || "18888999999".equals(tel)) { //是主播
            mIsShowGift = false;
        } else {
            mIsShowGift = true;
        }
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSendGiftEvent(SendGiftEvent sendGiftEvent) {
        try {
            SendGift sendGift = new Gson().fromJson(sendGiftEvent.mJson, SendGift.class);
            if (state == CallStateEnum.AUDIO.getValue()) { //语音
                avChatAudioUI.sendGiftSuc(sendGift);
            } else {
                avChatVideoUI.sendGiftSuc(sendGift);
            }
        } catch (Exception e) {
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    private int getResid(int id) {
        int resId = R.mipmap.gift001;
        switch (id) {
            case 1:
                resId = R.mipmap.gift001;
                break;
            case 2:
                resId = R.mipmap.gift002;
                break;
            case 3:
                resId = R.mipmap.gift003;
                break;
            case 4:
                resId = R.mipmap.gift004;
                break;
            case 5:
                resId = R.mipmap.gift005;
                break;
            case 6:
                resId = R.mipmap.gift006;
                break;
            case 7:
                resId = R.mipmap.gift007;
                break;
            case 8:
                resId = R.mipmap.gift008;
                break;
        }
        return resId;
    }

    private String getGiftName(int id) {
        String name = "fuck";
        switch (id) {
            case 1:
                name = "许愿气球";
                break;
            case 2:
                name = "面具";
                break;
            case 3:
                name = "陪你嗨";
                break;
            case 4:
                name = "爱情玫瑰";
                break;
            case 5:
                name = "买买买";
                break;
            case 6:
                name = "带你一起飞";
                break;
            case 7:
                name = "求婚钻戒";
                break;
            case 8:
                name = "在一起";
                break;
        }
        return name;
    }
}
